import sys, time
import spade, nltk
import sqlite3, re

import re, math, collections, itertools
import nltk, nltk.classify.util, nltk.metrics
from nltk.classify import NaiveBayesClassifier
from nltk.metrics import BigramAssocMeasures
from nltk.probability import FreqDist, ConditionalFreqDist
from nltk.corpus import wordnet

database_file = "Reviews_from_Crete.db"
database_path = "../datasets/"

#target.execute('''CREATE TABLE hotels (Hotel_ID text, Hotel_Name text, Hotel_Star real, Location_ID text, Location_Name text, 
#		Hotel_Rating real, Hotel_Clean real, Hotel_Service real, Hotel_Location real, Hotel_Rooms real, Hotel_Sleep real, Hotel_Value real,
#		Hotel_URL text)''')

#target.execute('''CREATE TABLE reviews (Review_ID text, Hotel_ID text, Review_Quote text, Review_Text text, Review_Rating real,
#		Review_Clean real, Review_Service real, Review_Location real, Review_Rooms real, Review_Sleep real, Review_Value real,
#		Review_URL text)''')

conn = sqlite3.connect(database_path + database_file)
source = conn.cursor()


reviews = []
hotels = []

for row in source.execute("SELECT * FROM reviews"):
	reviews.append(row)



topics = dict()
topics = {'room': ['room'], 'clean': ['clean'], 'staff': ['staff'], 'location': ['location'], 'value': ['value']}

r = []
for review in reviews[:5]:
	text = review[3]
	sentences = nltk.sent_tokenize(text)
	for sentence in sentences:
		tokens = nltk.word_tokenize(sentence)
		words = nltk.pos_tag(tokens)
		nouns = [w[0] for w in words if w[1].startswith("NN")]
		adjectives = [w[0] for w in words if w[1].startswith("JJ")]
		r.append([nouns,adjectives])
		for noun in nouns:
			nsyn = wordnet.synsets(noun)
			best_similarity = 0
			best_topic = 0
			if nsyn:
				for topic in topics:
					tsyn = wordnet.synsets(topic)
					if tsyn:
						similarity = nsyn[0].wup_similarity(tsyn[0]) # ίσως να ελέγξω όλα τα synsets?
					else: similarity = -1
					if similarity > best_similarity:
						best_similarity = similarity
						best_topic = topic
				if best_similarity < 0.3:
					if not noun in topics:
						topics[noun] = [noun]	# αν έχω ως index το synset?
				else:
					if not noun in topics[best_topic]:
						topics[best_topic].append(noun)


for t in topics:
	print t, topics[t]



res = {u'distance': [u'distance', u'part', u'hardwood'], 'room': ['room', u'hotel', u'rooms', u'pane', u'beds', u'mattresses', u'mushrooms', u'nuts', u'fruits', u'screens', u'hotels', u'screen', u'tv', u'bathrooms', u'Well', u'Hotel', u'floors', u'wall', u'bathroom', u'restaurant', u'gem', u'TV', u'shower', u'street', u'car'], u'seamless': [u'seamless', u'warm', u'busy'], 'value': ['value', u'lit', u'experience', u'taste', u'acoustics', u'issues', u'midnight', u'fare'], u'years': [u'years', u'parking', u'months', u'morning', u'euro'], u'amazing': [u'amazing'], u'comfy': [u'comfy'], u'health': [u'health', u'sound', u'problem', u'First', u'level'], 'location': ['location', u'Samaria', u'glass', u'front', u'back', u'Greece', u'guests', u'breakfast', u'eggs', u'sausages', u'eater', u'foods', u'yogurts', u'cheeses', u'Crete', u'grains', u'attendees', u'owners', u'oasis', u'Have', u'center', u'town', u'Nice', u'Ca', u'city', u'travelers', u'area', u'Star', u'Europe', u'hardwood', u'Venetian', u'touts', u'waterfront', u'king', u'Gorge', u'middle'], 'clean': ['clean', u'time', u'try', u'renovation', u'work', u'presentations', u'recreation', u'Service', u'service', u'project', u'renovations', u'walking', u'case', u'hike', u'noise', u'transfer', u'discount'], u'excellent': [u'excellent'], u'a.m.': [u'a.m.'], u'minutes': [u'minutes', u'Kudos', u'meaning', u'notice'], 'staff': ['staff', u'business', u'times', u'personnel', u'conference', u'group', u'collection']}

def similarity(word1, word2):
	nsyn = wordnet.synsets(word1)
	tsyn = wordnet.synsets(word2)
	for n in nsyn:
		for t in tsyn:
			print n, t, n.wup_similarity(t)

similarity('breakfast','honey')


